package com.lcx.security.FFmpegCommandManager;



import com.lcx.security.dao.TaskDaoImpl;
import com.lcx.security.entity.TaskEntity;
import com.lcx.security.main.FFmpegCMD;
import com.lcx.security.myenum.StreamEnum;
import com.lcx.security.service.*;
import com.lcx.security.service.impl.CommandAssemblyImpl;
import com.lcx.security.service.impl.DefaultOutHandlerMethod;
import com.lcx.security.service.impl.TaskHandlerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Iterator;

/**
 * FFmpeg命令操作管理器
 * 
 */
public class FFmpegManagerImpl implements FFmpegManager {

	protected static Logger logger = LoggerFactory.getLogger(FFmpegManager.class);
	/**
	 * 任务持久化器
	 */
	private TaskDaoImpl taskDao = null;
	/**
	 * 任务执行处理器
	 */
	private TaskHandler taskHandler = null;
	/**
	 * 命令组装器
	 */
	private CommandAssembly commandAssembly = null;
	/**
	 * 任务消息处理器
	 */
	private OutHandlerMethod ohm = null;

	public FFmpegManagerImpl() {
		init(null);
	}


	/**
	 * 初始化，如果几个处理器未注入，则使用默认处理器
	 * 
	 * @param size
	 */
	public void init(Integer size) {

		if (this.ohm == null) {
			this.ohm = new DefaultOutHandlerMethod();
		}
		if (this.taskDao == null) {
			this.taskDao = new TaskDaoImpl();
		}
		if (this.taskHandler == null) {
			this.taskHandler = new TaskHandlerImpl(this.ohm);
		}
		if (this.commandAssembly == null) {
			this.commandAssembly = new CommandAssemblyImpl();
		}
	}

	public void setTaskDao(TaskDaoImpl taskDao) {
		this.taskDao = taskDao;
	}

	public void setTaskHandler(TaskHandler taskHandler) {
		this.taskHandler = taskHandler;
	}

	public void setCommandAssembly(CommandAssembly commandAssembly) {
		this.commandAssembly = commandAssembly;
	}

	public void setOhm(OutHandlerMethod ohm) {
		this.ohm = ohm;
	}

	/**
	 * 是否已经初始化
	 * 
	 * @param b 如果未初始化时是否初始化
	 * @return
	 */
	public boolean isInit(boolean b) {
		boolean ret = this.ohm == null || this.taskDao == null || this.taskHandler == null|| this.commandAssembly == null;
		if (ret && b) {
			init(null);
		}
		return ret;
	}

	@Override
	public String start(StreamEnum streamEnum, String command) {
		return start(streamEnum, command, false);
	}

	@Override
	public String start(StreamEnum streamEnum, String command, boolean hasPath) {
		if (isInit(true)) {
			logger.error("执行失败，未进行初始化或初始化失败！");
			return null;
		}
		if (command != null) {
			TaskEntity tasker = taskHandler.process(streamEnum.getName(), hasPath ? command :  command);
			if (tasker != null) {
				int ret = taskDao.add(tasker);
				if (ret > 0) {
					return tasker.getId();
				} else {
					// 持久化信息失败，停止处理
					taskHandler.stop(tasker);
					logger.error("持久化失败，停止任务！");
					return tasker.getId();
				}
			}
		}
		return null;
	}


	@Override
	public boolean stop(String id) {
		if (id != null && taskDao.isHave(id)) {
			logger.info("正在停止任务：" + id);
			TaskEntity tasker = taskDao.get(id);
			if (taskHandler.stop(tasker)) {
				taskDao.remove(id);
				return true;
			}
		}
		logger.error("停止任务失败！id=" + id);
		return false;
	}

	@Override
	public int stopAll() {
		Collection<TaskEntity> list = taskDao.getAll();
		Iterator<TaskEntity> iter = list.iterator();
		TaskEntity tasker = null;
		int index = 0;
		while (iter.hasNext()) {
			tasker = iter.next();
			if (taskHandler.stop(tasker)) {
				taskDao.remove(tasker.getId());
				index++;
			}
		}

		logger.info("停止了" + index + "个任务！");
		return index;
	}

	@Override
	public TaskEntity query(String id) {
		return taskDao.get(id);
	}

	@Override
	public Collection<TaskEntity> queryAll() {
		return taskDao.getAll();
	}
}
